﻿using System;
using System.Collections.Generic;
using System.Data ;
using System.Data.SqlClient ;
using System.Linq;
using System.Text;
using BoYuan.DBServices ;
using SqlSugar ;

namespace BoYuan.DBServices
{
    /// <summary>
	/// 数据访问类:sysModule
	/// </summary>
	public class SysModule
    {
        /// <summary>
        /// 更新字体图标
        /// </summary>
        /// <param name="fontIcon">字体图标</param>
        /// <param name="id"></param>
        /// <returns></returns>
        public static bool UpdateFontIcon(string fontIcon,int id)
        {
            var db = DB_Base.Instance;
            Entity.sysModule mo = new Entity.sysModule();
            mo.fontIcon = fontIcon == "None" ? string.Empty : fontIcon;
            mo.moduleIcon = string.Empty;
            
            return db.Updateable<Entity.sysModule>()
                       .SetColumns(p => new Entity.sysModule { fontIcon= mo.fontIcon, moduleIcon= mo.moduleIcon })
                       .Where(p => p.id == id).ExecuteCommand() > 0;
        }

        /// <summary>
        /// 更新绝对路径
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
	    public static bool UpdateModulePath(int id)
        {
            var db = DB_Base.Instance;
            //string sql = string.Format(@"UPDATE sysModule
            //                                    SET	
            //                                     modulePath = modulePath+'{0},'
            //                                    WHERE id={0}", id);
            //return db.Ado.ExecuteCommand(sql) > 0;

            string strId = id.ToString();
            return db.Updateable<Entity.sysModule>()
                       .UpdateColumns(p => new { p.modulePath }).ReSetValue(p => p.modulePath == (p.modulePath + strId + ","))
                       .Where(p => p.id == id).ExecuteCommand() > 0;
        }

        /// <summary>
        /// 更新父节点为不是跟节点
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
	    public static bool UpdateParentNoEnd(int id)
        {
            var db = DB_Base.Instance;
            string sql = string.Format(@"UPDATE sysModule
                                    SET	
                                         isend=0
                                WHERE id=(SELECT parentID FROM sysModule AS sm WHERE sm.id={0} )", id);
            return db.Ado.ExecuteCommand(sql) > 0;
        }

        /// <summary>
        /// 得到一个对象实体(父级的绝对路径)
        /// </summary>
        public static Entity.sysModule GetModulePathModel(int id)
        { 
            var db = DB_Base.Instance ;
            //获取父级的绝对路径

            var query = db.Queryable<Entity.sysModule>()
                .Where(p => p.id == id)
                .Select(p => new Entity.sysModule
                {
                    id = p.id,
                    parentID = p.parentID,
                    moduleName = p.moduleName,
                    modulePath = SqlFunc.Subqueryable<Entity.sysModule>().Where(m=>m.id==p.parentID).Select(m=>m.modulePath),
                    moduleUrl = p.moduleUrl,
                    moduleIcon = p.moduleIcon,
                    sort = p.sort,
                    status = p.status,
                    info = p.info,
                    isEnd = p.isEnd,
                });

            return query.First() ;
        }

        /// <summary>
        /// 页面角色id 获取页面组件数据
        /// </summary>
        /// <param name="sysRolepopedom_id">页面权限id</param>
        /// <returns></returns>
        public static DataSet GetListBySysRolepopedomId(int sysRolepopedom_id)
        {
            var db = DB_Base.Instance;
            string sql = @"SELECT    sm.*
                                            FROM   SysModule AS sm
                                            WHERE  (
                                                       SELECT ',' + moduleid + ','
                                                       FROM   SysRolepopedom AS sr
                                                       WHERE  sr.id = @id
                                                   ) LIKE '%,' + CAST(sm.id AS VARCHAR(20)) + ',%' ";

            return db.Ado.GetDataSetAll(sql, new { id = sysRolepopedom_id });
        }

        /// <summary>
        /// 判断页面权限
        /// </summary>
        /// <param name="rid">权限角色id</param>
        /// <param name="url">当前页面url</param>
        /// <returns></returns>
        public static bool IsRole(int rid, string url)
        {
            var db = DB_Base.Instance;
            string sql = @"SELECT COUNT(1)
                            FROM   SysModule AS sm
                            WHERE  (
                                       SELECT ',' + moduleid + ','
                                       FROM   SysRolepopedom AS sr
                                       WHERE  sr.id = @id
                                   ) LIKE '%,' + CAST(sm.id AS VARCHAR(20)) + ',%' and moduleUrl=@moduleUrl  ";

            object obj= db.Ado.GetScalar(sql, new {id = rid, moduleUrl = url}) ;
         
            if (obj == null)
            {
                return false;
            }
            else
            {
                return Convert.ToInt32(obj) > 0;
            }
        }
    }
}
